package org.studle.data;

import java.util.HashMap;
import java.util.Map;

import org.studle.domain.exceptions.*;

/**
 * Contains general methods for DAO-layer classes
 * @author Nasty
 * 
 */
public class GenericDAO<K, V> {	
	private Map<K,V> dataSet = new HashMap<K, V>();

	public void createRecord(K key, V element) throws DuplicateKeyException, IllegalArgumentException{		
		if(element == null || key == null) throw new IllegalArgumentException("Null reference passed as a parameter to createRecord()");  
		if(dataSet.containsKey(key)) throw new DuplicateKeyException(key);
		
		dataSet.put(key, element);
	}
	
	public void deleteRecord(K key) throws NonExistentElementException, IllegalArgumentException{
		if(key == null) throw new IllegalArgumentException("trying to delete null element");
		if (!dataSet.containsKey(key)) throw new NonExistentElementException(key);
		dataSet.remove(key);
	}
	
	public V getRecordByPrimaryKey(K key) throws NonExistentElementException{
		if (!dataSet.containsKey(key)) throw new NonExistentElementException(key);
		return dataSet.get(key);
	}
	
}
